<?php

class Core_Backend_Component_Permission_Controller_Account extends Dcmp_Component_Controller {

    public function indexAction($pageindex = 1, $pagesize = 20, $username = NULL) {
        $this->getHistory()->record();
        $collection = App::getModel('permission/collection_account')
                        ->pagination($pageindex, $pagesize);
        if (!empty($username)) {
            $collection->where()->like('a.user_name', ':username:s')->closest()
                    ->addParameter('username', "%$username%");
        }

        $collection->load();
        $records = $collection->loadCount();
        return $this->viewResult(array('list' => $collection, 'records' => $records));
    }

    public function createAction() {
        $roles = App::getModel('permission/collection_role')->load();    	
        return $this->viewResult(array('roles'=>$roles));
    }

    public function editAction($id) {
        $model = App::getModel('permission/account')->load($id);
        return $this->createAction()->setModel($model)->setView('edit');
    }
    
    public function batcheditAction($ids){
        $accounts = App::getModel('permission/collection_account')
						->filterByIds($ids)
        				->load();
        return $this->createAction()->setAccounts($accounts);    	
    }
    
    public function batchsaveAction($ids){
        $accounts = App::getModel('permission/collection_account')
						->filterByIds($ids)
        				->load();    
        $data = $this->getRequest()->getForm('account');  				
        foreach($accounts as $account){
        	$account->addData($data);
        }        				
        $this->getMessager()->addInform($this->__('share.message.success'));
        return $this->redirectResult($this->getHistoryUrl('permission/account/index'));         
    }    

    public function savePostAction() {
        $data = $this->getRequest()->getForm('account');
        $model = App::getModel('permission/account');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
        }
        if(isset($data['password'])){
            $data['password'] = md5($data['password']);
        }
        $model->addData($data);
        try{
        	$model->validate();
            $model->save();
            $this->getMessager()->addInform($this->__('share.message.success'));
            return $this->redirectResult($this->getHistoryUrl('permission/account/index'));        	
        }catch(Core_Model_Permission_Exception_Account $ex){
          	$this->getMessager()->addError($ex->getMessage());
            return $this->createAction()->setActionName('create')->setModel($model);        	
        }
    }

    public function updateStatusAction($id, $status) {
        App::getModel('permission/account')
                ->load($id)
                ->setStatus($status == 'enabled' ? 'disabled' : 'enabled')
                ->save();
        return $this->redirectResult($this->getHistoryUrl('permission/account/index'));
    }

}